package com.fengmi.controller;

import com.fengmi.entity.SysUserEntity;
import com.fengmi.service.SysUserService;
import com.fengmi.util.JwtUtils;
import com.fengmi.util.RsaUtils;
import com.fengmi.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;

/**
 * Controller：用户信息表
 *
 * @author
 * @date 2022-02-10 00:23:01
 */
@RestController
@RequestMapping("user")
@CrossOrigin
@Api(tags = "用户管理")
public class SysUserController{
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("login")
    @ApiOperation("登录认证")
    private ResultVO login(@RequestBody SysUserEntity userEntity){
        return sysUserService.login(userEntity);
    }


    @PostMapping("check")
    @ApiOperation("令牌认证")
    private ResultVO check(@RequestHeader String token){
        try {
            //公钥认证
            PublicKey publicKey = RsaUtils.getPublicKey(ResourceUtils.getFile("classpath:rsa.pub").getPath());
            SysUserEntity sysUser = (SysUserEntity) JwtUtils.getInfoFromToken(token, publicKey, SysUserEntity.class);

            //没发生异常,令牌合法
            return new ResultVO(true, "合法令牌" ,sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            //发生异常,非法令牌
            return new ResultVO(false, "非法令牌");
        }
    }
}
